package view.component.weapon.flute
{
	import enums.main.UISize;
	import enums.weapon.WeaponViewSCPageIndex;
	
	import feathers.controls.Button;
	import feathers.controls.ButtonGroup;
	import feathers.controls.Screen;
	import feathers.controls.ScrollContainer;
	import feathers.controls.Scroller;
	import feathers.data.ListCollection;
	
	import starling.animation.Tween;
	import starling.core.Starling;
	import starling.events.Event;
	
	import util.FG2FUGenerator;
	
	import view.component.IAnimateNaviView;
	import view.component.weapon.WeaponOtherInfoView;
	
	
	/**
	 * 狩猎笛
	 * @author £iang
	 * 
	 */	
	public class FluteView extends Screen implements IAnimateNaviView
	{
		//-----------------------------
		// variables
		//-----------------------------
		
		/** 内容滚动容器 */
		public var mainSC:ScrollContainer;
		/** 页面切换 */
		public var mainTabBar:ButtonGroup;
		/** 狩猎笛简表界面 */
		public var listView:FluteListView;
		/** 狩猎笛详细情报界面 */
		public var detailView:FluteDetailView;
		/** 狩猎笛搜索界面 */
		public var searchView:FluteSearchView;
		/** 狩猎笛动作值界面 */
		public var motionView:FluteMotionView;
		/** 其他数值界面 */
		public var otherView:WeaponOtherInfoView;
		/** 狩猎笛乐谱界面 */
		public var staffView:FluteStaffView;
		/** 返回详细界面按钮 */
		public var returnDetailBtn:Button;
		
		//-----------------------------
		// properties
		//-----------------------------
		
		//-----------------------------
		// setter&getter
		//-----------------------------
		
		//-----------------------------
		// methods
		//-----------------------------
		
		/**
		 * 初始化函数 
		 * 
		 */		
		override protected function initialize():void
		{
			var labelVec:Vector.<String> = new Vector.<String>();
			var dataProvider:ListCollection = new ListCollection( [] );
			var funcVec:Vector.<Function> = new Vector.<Function>();
			
			labelVec.push( "简表", "搜索", "动作值", "其他", "乐谱" );
			funcVec.push( listTriggeredHandler, searchTriggeredHandler, motionTriggeredHandler, otherTriggeredHandler, staffTriggeredHandler );
			mainTabBar = new ButtonGroup();
			mainTabBar.direction = ButtonGroup.DIRECTION_HORIZONTAL;
			mainTabBar.gap = ( UISize.MAIN_W - ( 40 + UISize.TAB_BUTTON_W3 * 5 ) * this.dpiScale ) / 4;
			mainTabBar.alpha = 0;
			
			for ( var i:int = 0; i < 5; i ++ )
			{
				var obj:Object = new Object();
				obj.label = labelVec[ i ];
				obj.triggered = funcVec[ i ];
				dataProvider.push( obj );
			}
			
			mainTabBar.dataProvider = dataProvider;
			mainTabBar.buttonFactory = mainTabFactory;
			this.addChild( mainTabBar );
			
			mainSC = new ScrollContainer();
			mainSC.scrollerProperties.hasElasticEdges = false;
			mainSC.scrollerProperties.snapToPages = true;
			mainSC.scrollerProperties.throwEase = "easeOut";
			mainSC.scrollerProperties.interactionMode = 
				Scroller.INTERACTION_MODE_MOUSE;
			mainSC.scrollerProperties.scrollBarDisplayMode = 
				Scroller.SCROLL_BAR_DISPLAY_MODE_NONE;
			mainSC.width = UISize.MAIN_W;
			mainSC.height = UISize.MAIN_H - 
				UISize.MONSTER_TAB_AREA_H * this.dpiScale * 2;
			this.addChild( mainSC );
			
			listView = new FluteListView();
			listView.width = UISize.MAIN_W;
			mainSC.addChild( listView );
			
			detailView = new FluteDetailView();
			detailView.width = UISize.MAIN_W;
			mainSC.addChild( detailView );
			
			searchView = new FluteSearchView();
			searchView.width = UISize.MAIN_W;
			mainSC.addChild( searchView );
			
			motionView = new FluteMotionView();
			motionView.width = UISize.MAIN_W;
			mainSC.addChild( motionView );
			
			otherView = new WeaponOtherInfoView();
			otherView.width = UISize.MAIN_W;
			mainSC.addChild( otherView );
			
			staffView = new FluteStaffView();
			staffView.width = UISize.MAIN_W; 
			mainSC.addChild( staffView );
			
			returnDetailBtn = mainTabFactory();
			returnDetailBtn.label = "返回";
			returnDetailBtn.alpha = 0;
			returnDetailBtn.visible = false;
			this.addChild( returnDetailBtn );
			
			addListener();
		}
		
		/**
		 * 绘制函数 
		 * 
		 */		
		override protected function draw():void
		{
			mainTabBar.y = UISize.MAIN_H - UISize.MONSTER_TAB_AREA_H * this.dpiScale * 2 
				+ 20 * this.dpiScale;
			mainTabBar.x = 20 * this.dpiScale;
			returnDetailBtn.x = 20 * this.dpiScale;
			returnDetailBtn.y = mainTabBar.y;
			detailView.x = UISize.MAIN_W;
			searchView.x = UISize.MAIN_W * 2;
			motionView.x = UISize.MAIN_W * 3;
			otherView.x = UISize.MAIN_W * 4;
			staffView.x = UISize.MAIN_W * 5;
		}
		
		/**
		 * 添加侦听 
		 * 
		 */		
		public function addListener():void
		{
			returnDetailBtn.addEventListener( Event.TRIGGERED, returnDetailTriggeredHandler );
		}
		
		/**
		 * 显示动画
		 * 
		 */		
		public function playShowAnimate():void
		{
			listView.playShowAnimate();
			
			var tween:Tween = new Tween( mainTabBar, 0.3 );
			
			tween.animate( "alpha", 1 );
			Starling.juggler.add( tween );
		}
		
		/**
		 * 隐藏动画
		 * 
		 */		
		public function playHideAnimate():void
		{
			switch(mainSC.horizontalPageIndex)
			{
				case WeaponViewSCPageIndex.LIST_PAGE:
				{
					listView.playHideAnimate();
					break;
				}
					
				case WeaponViewSCPageIndex.DETAIL_PAGE:
				{
					detailView.playHideAnimation();
					break;
				}
					
				case WeaponViewSCPageIndex.SEARCH_PAGE:
				{
					searchView.playHideAnimation();
					break;
				}
					
				case WeaponViewSCPageIndex.MOTION_PAGE:
				{
					motionView.playHideAnimate();
					break;
				}
					
				case WeaponViewSCPageIndex.OTHER_PAGE:
				{
					otherView.playHideAnimation();
					break;
				}
					
				case WeaponViewSCPageIndex.STAFF_PAGE:
				{
					staffView.playHideAnimation();
					break;
				}
					
				default:
				{
					break;
				}
			}
			
			var tween:Tween = new Tween( mainTabBar, 0.3 );
			
			tween.animate( "alpha", 0 );
			Starling.juggler.add( tween );
		}
		
		/**
		 * 改变滚动条容器页面 
		 * @param newPage 新页面
		 * 
		 */		
		public function changeSCPageIndex( newPage:int ):void
		{
			mainSC.scrollToPageIndex( newPage, 0, 0.4 );	
		}
		
		/**
		 * 批量设置mainTabBar按钮 
		 * @return tabbar按钮
		 * 
		 */		
		private function mainTabFactory():Button
		{
			var button:Button = FG2FUGenerator.generateRoundRectButton(
				UISize.TAB_BUTTON_W3, UISize.TAB_BUTTON_H,
				this.dpiScale, UISize.TAB_BUTTON_H * 0.9, "small"
			);
			
			return button;
		}
		
		/**
		 * 前往旋律页并且隐藏按钮导航显示返回按钮 
		 * 
		 */		
		public function gotoMelody():void
		{
			changeSCPageIndex( WeaponViewSCPageIndex.STAFF_PAGE );
			mainTabBar.touchable = false;
			returnDetailBtn.visible = true;
			
			var tween:Tween = new Tween( mainTabBar, 0.3 );
			
			tween.animate( "alpha", 0 );
			tween.onComplete = function ():void{
				mainTabBar.touchable = true;
				mainTabBar.visible = false;
			}
			Starling.juggler.add( tween );
			
			var tween2:Tween = new Tween( returnDetailBtn, 0.3 );
			
			tween2.animate( "alpha", 1 );
			Starling.juggler.add( tween2 );
		}
	
		//-----------------------------
		// events
		//-----------------------------
		
		/**
		 * 处理点击列表按钮派发的事件 
		 * @param event 点击列表按钮派发的事件 
		 * 
		 */		
		protected function listTriggeredHandler( event:Event ):void
		{
			changeSCPageIndex( WeaponViewSCPageIndex.LIST_PAGE );
		}
		
		/**
		 * 处理点击搜索按钮派发的事件 
		 * @param event 点击搜索按钮派发的事件 
		 * 
		 */		
		protected function searchTriggeredHandler( event:Event ):void
		{
			changeSCPageIndex( WeaponViewSCPageIndex.SEARCH_PAGE );
		}
		
		/**
		 * 处理点击动作值按钮派发的事件 
		 * @param event 点击动作值按钮派发的事件 
		 * 
		 */		
		protected function motionTriggeredHandler( event:Event ):void
		{
			changeSCPageIndex( WeaponViewSCPageIndex.MOTION_PAGE );
		}
		
		/**
		 * 处理点击其他按钮派发的事件 
		 * @param event 点击其他按钮派发的事件 
		 * 
		 */		
		protected function otherTriggeredHandler( event:Event ):void
		{
			changeSCPageIndex( WeaponViewSCPageIndex.OTHER_PAGE );
		}
		
		/**
		 * 处理点击乐谱按钮派发的事件 
		 * @param event 点击乐谱按钮派发的事件 
		 * 
		 */		
		protected function staffTriggeredHandler( event:Event ):void
		{
			changeSCPageIndex( WeaponViewSCPageIndex.STAFF_PAGE );
		}
		
		/**
		 * 处理点击返回详细按钮后派发的事件 
		 * @param event 点击返回详细按钮后派发的事件 
		 * 
		 */		
		protected function returnDetailTriggeredHandler( event:Event ):void
		{
			changeSCPageIndex( WeaponViewSCPageIndex.DETAIL_PAGE );
			returnDetailBtn.touchable = false;
			mainTabBar.visible = true;
			
			var tween:Tween = new Tween( returnDetailBtn, 0.3 );
			
			tween.animate( "alpha", 0 );
			tween.onComplete = function ():void{
				returnDetailBtn.touchable = true;
				returnDetailBtn.visible = false;
			}
			Starling.juggler.add( tween );
			
			var tween2:Tween = new Tween( mainTabBar, 0.3 );
			
			tween2.animate( "alpha", 1 );
			Starling.juggler.add( tween2 );
		}
		
		//-----------------------------
		// dispose
		//-----------------------------
		
		/**
		 * 析构函数 
		 * 
		 */		
		override public function dispose():void
		{
			Starling.juggler.removeTweens( mainTabBar );
			Starling.juggler.removeTweens( returnDetailBtn );
			
			var button:Button;
			var k:int;
			
			for ( k = 0; k < mainTabBar.numChildren; k ++ )
			{
				if ( mainTabBar.getChildAt( k ) is Button )
				{
					button = mainTabBar.getChildAt( k ) as Button;
					button.dispose();
					FG2FUGenerator.deepDisposeButtonComp( button );
					button = null;
				}
			}
			
			
			mainTabBar.dispose();
			mainTabBar = null;
			returnDetailBtn.dispose();
			FG2FUGenerator.deepDisposeButtonComp( returnDetailBtn );
			returnDetailBtn = null;
			super.dispose();
		}
		
		//-----------------------------
		// construction
		//-----------------------------
		
		/**
		 * 构造函数
		 *
		 */
		public function FluteView()
		{
			super();
		}
	}
}